<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="View.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Administrator view ToWa webshop</title>
</head>

<%
	ArrayList<CategoryView> cat = Controller.getCategoryTree();
	ItemView item = (ItemView) request.getAttribute("item");
	UserView user = (UserView) request.getAttribute("userInfo");
	UserView requestingUser = (UserView) request.getSession().getAttribute("user");
	
	if (requestingUser.getRank() != 0) {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
		dispatcher.forward(request, response);
	}
%>
<body>
	<div id="item">
		<form name="item_form1" action="admin" method="post">
			<table>
				<tr>
					<td><h1>Item</h1></td>
				</tr>
				<tr>
					<td>Fetch item by id:</td><td><input type="text" name="itemTextField" /></td><td><input type="submit" value="Fetch" /></td>
				</tr>
			</table>
			<input type="hidden" name="hiddenField" value="0" />
		</form>
		<form name="item_form2" action="admin" method="post">
			<table>
				<tr>
					<td>id</td><td>name</td><td>category</td><td>amount</td><td>description</td><td>price</td><td>Add</td>
				</tr>
				<tr>
					<td><input type="text" name="id_text" value="<%if (item != null) {%><%=item.getItemId()%><%}%>" /></td>
					<td><input type="text" name="name_text" value="<%if (item != null) {%><%=item.getName()%><%}%>" /></td>
					<td><select name="item_dropdown">
						
						<%
						if(cat != null){
							for(int i = 0; i < cat.size(); i++){
						%>
								<option value="<%=cat.get(i).getNr()%>" <%if (item != null) { if (item.getCategory() == cat.get(i).getNr()) {%>selected="selected"<%}}%>><%=cat.get(i).getName()%></option>
								<%
								ArrayList<CategoryView> sub = cat.get(i).getChild();
	    						for(int j = 0; j < sub.size(); j++){   
								%>
									<option value="<%=sub.get(j).getNr()%>" <%if (item != null) { if (item.getCategory() == sub.get(j).getNr()) {%>selected="selected"<%}}%>>&nbsp;&nbsp;<%=sub.get(j).getName()%></option>
								<%
									ArrayList<CategoryView> subsub = sub.get(j).getChild();
									for(int k = 0; k < subsub.size(); k++){
								%>
										<option value="<%=subsub.get(k).getNr()%>" <%if (item != null) { if (item.getCategory() == subsub.get(k).getNr()) {%>selected="selected"<%}}%>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%=subsub.get(k).getName()%></option>
												<%
									}
								}   
							}
						}
						%>
					</select></td>
					<td><input type="text" name="amount_text" value="<%if (item != null) {%><%=item.getAmount()%><%}%>"/></td>
					<td><textarea name="description_text" rows="5" cols="40"><%if (item != null) {%><%=item.getDescription()%><%}%></textarea></td>
					<td><input type="text" name="price_text" value="<%if (item != null) {%><%=item.getPrice()%><%}%>"/></td>
					<td><input type="submit" value="Add/Update"/></td>
				</tr>
			</table>
			<input type="hidden" name="hiddenField" value="1" />
		</form>
	</div>
	<div id="category">
		<form name="category_form1" action="admin" method="post">
			<table>
				<tr>
					<td><h1>Category</h1></td>
				</tr>
				<tr>
					<td><select name="removeCategoryDropdown">
					<%
					if(cat != null){
						for(int i = 0; i < cat.size(); i++){
					%>
							<option value="<%=cat.get(i).getNr()%>"><%=cat.get(i).getName()%></option>
							<%
							ArrayList<CategoryView> sub = cat.get(i).getChild();
							for(int j = 0; j < sub.size(); j++){   
							%>
								<option value="<%=sub.get(j).getNr()%>">&nbsp;&nbsp;<%=sub.get(j).getName()%></option>
								<%  
								ArrayList<CategoryView> subsub = sub.get(j).getChild();
								for(int k = 0; k < subsub.size(); k++){
								%>
									<option value="<%=subsub.get(k).getNr()%>">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%=subsub.get(k).getName()%></option>
					<%
								}   
							}
						}
					}
					%>
					</select></td>
					<td><input type="submit" value="Remove" /></td>
				</tr>
			</table>
			<input type="hidden" name="hiddenField" value="2" />
		</form>
		<form name="category_form2" action="admin" method="post">
			<table>
				<tr>
					<td><select name="addCategoryDropdown">
					<%
					if(cat != null){
						for(int i = 0; i < cat.size(); i++){
					%>
							<option value="<%=cat.get(i).getNr()%>"><%=cat.get(i).getName()%></option>
							<%
							ArrayList<CategoryView> sub = cat.get(i).getChild();
							for(int j = 0; j < sub.size(); j++){   
							%>
								<option value="<%=sub.get(j).getNr()%>">&nbsp;&nbsp;<%=sub.get(j).getName()%></option>
					<%
							}
						}
					}
					%>
					</select></td>
					<td><input type="text" name="subMenu_text" /></td>
					<td><input type="submit" value="Add" /></td>
				</tr>
			</table>
			<input type="hidden" name="hiddenField" value="3" />
		</form>
		<form name="category_form3" action="admin" method="post">
			<table>
				<tr>
					<td><select name="editCategoryDropdown">
					<%
					if(cat != null){
						for(int i = 0; i < cat.size(); i++){
					%>
							<option value="<%=cat.get(i).getNr()%>"><%=cat.get(i).getName()%></option>
							<%
							ArrayList<CategoryView> sub = cat.get(i).getChild();
							for(int j = 0; j < sub.size(); j++){   
							%>
								<option value="<%=sub.get(j).getNr()%>">&nbsp;&nbsp;<%=sub.get(j).getName()%></option>
								<%  
								ArrayList<CategoryView> subsub = sub.get(j).getChild();
								for(int k = 0; k < subsub.size(); k++){
								%>
									<option value="<%=subsub.get(k).getNr()%>">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%=subsub.get(k).getName()%></option>
					<%
								}   
							}
						}
					}
					%>
					</select></td>
					<td><input type="text" name="editCategory_text" /></td>
					<td><input type="submit" value="update"/></td>
				</tr>
			</table>
			<input type="hidden" name="hiddenField" value="6" />
		</form>
	</div>
	<div id="user">
		<form name="user_form1" action="admin" method="post">
			<table>
				<tr>
					<td><h1>User</h1></td>
				</tr>
				<tr>
					<td>Fetch user by username:</td>
					<td><input type="text" name="getUsername_text" /></td>
					<td><input type="submit" value="Fetch" /></td>
				</tr>
			</table>
			<input type="hidden" name="hiddenField" value="4" />
		</form>
		<form name="user_form2" action="admin" method="post">
			<table>
				<tr>
					<td>Id</td><td><input type="text" name="id_text" value="<%if (user != null) {%><%=user.getUserId()%><%}%>"/></td>
				</tr>
				<tr>
					<td>username</td><td><input type="text" name="username_text" value="<%if (user != null) {%><%=user.getUsername()%><%}%>"/></td>
				</tr>
				<tr>
					<td>rank</td><td><input type="text" name="rank_text" value="<%if (user != null) {%><%=user.getRank()%><%}%>"/></td>
				</tr>
				<tr>
					<td>address</td><td><input type="text" name="address_text" value="<%if (user != null) {%><%=user.getAddress()%><%}%>"/></td>
				</tr>
				<tr>
					<td>zipcode</td><td><input type="text" name="zipcode_text" value="<%if (user != null) {%><%=user.getZipcode()%><%}%>"/></td>
				</tr>
				<tr>
					<td>city</td><td><input type="text" name="city_text" value="<%if (user != null) {%><%=user.getCity()%><%}%>"/></td>
				</tr>
				<tr>
					<td>email</td><td><input type="text" name="email_text" value="<%if (user != null) {%><%=user.getEmail()%><%}%>"/></td>
				</tr>
				<tr>
					<td>firstname</td><td><input type="text" name="firstname_text" value="<%if (user != null) {%><%=user.getFirstname()%><%}%>"/></td>
				</tr>
				<tr>
					<td>surname</td><td><input type="text" name="surname_text" value="<%if (user != null) {%><%=user.getSurname()%><%}%>"/></td>
				</tr>
				<tr>
					<td><input type="submit" value="update" /></td>
				</tr>
			</table>
			<input type="hidden" name="hiddenField" value="5" />
		</form>
	</div>
</body>
</html>